﻿using System;
using System.Collections.Generic;
using System.Text;

namespace ycdlib
{
	/// <summary>
	/// 浮動小数のYCNode
	/// </summary>
	internal class YCNodeFloat : YCNodeBase
	{
		#region フィールド

		private double val;

		#endregion

	
		#region YCNode メンバ

		/// <summary>
		/// YC型の名前は“int”
		/// </summary>
		public override string YCType
		{
			get { return "float"; }
			internal set { throw new NotSupportedException(); }
		}

		/// <summary>
		/// 値を取得、または設定できる
		/// </summary>
		public override object Value
		{
			get
			{
				return val;
			}
			set
			{
				if( value == null )
				{
					val = 0;
				}else
				{
					if( value is double )
					{
						val = (double)value;
					}else if( value is int )
					{
						val = (double)(int)value;
					}else if( value is float )
					{
						val = (double)(float)value;
					}else
					{
						string s = value.ToString().Replace( ",", "" );
						val = double.Parse( s );
					}
				}
			}
		}




		/// <summary>
		/// 子供の数は常にゼロ
		/// </summary>
		public override int ChildCount
		{
			get { return 0; }
		}


		public override void Write ( System.IO.Stream s, int p )
		{
			Encoding u = Encoding.UTF8;

			StringBuilder sb = new StringBuilder();
			while( p > 0 )
			{
				sb.Append( "	" );
				--p;
			}
			sb.Append( "float " )
				.Append( Name );
			if( !string.IsNullOrEmpty(ID) )
				sb.Append( ":" ).Append( ID );
			sb.Append( "(" )
				.Append( val )
				.Append( ")\n" );

			byte[] buff = u.GetBytes( sb.ToString() );
			s.Write( buff, 0, buff.Length );
		}


		/// <summary>
		/// 子供を持たないため、このメソッドはサポートされません
		/// </summary>
		/// <param name="newNode"></param>
		public override void AddChild ( YCNode newNode )
		{
			throw new NotSupportedException();
		}



		#endregion


		/// <summary>
		/// コンストラクタ
		/// </summary>
		/// <param name="p"></param>
		public YCNodeFloat ( YCNode p ) : base(p)
		{
		}


		static public YCNode CreateNode ( YCNode p ) { return new YCNodeFloat( p ); }
	}
}
